package com.bnyy.medicalHousekeeper.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

public abstract class RefreshFragment extends BaseFragmentImpl {

    private OnRefreshListener onRefreshListener;
    private OnLoadMoreListener onLoadMoreListener;

    public abstract OnRefreshListener registerOnRefreshListener();

    public abstract OnLoadMoreListener registerOnLoadMoreListener();

    public SmartRefreshLayout mRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        mRefreshLayout = new SmartRefreshLayout(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mRefreshLayout.setLayoutParams(params);

        mRefreshLayout.setEnableOverScrollDrag(true);
        mRefreshLayout.setDragRate(0.25f);

        onRefreshListener = registerOnRefreshListener();
        mRefreshLayout.setEnableRefresh(onRefreshListener != null);
        mRefreshLayout.setOnRefreshListener(onRefreshListener);

        onLoadMoreListener = registerOnLoadMoreListener();
        mRefreshLayout.setEnableLoadMore(onLoadMoreListener != null);
        mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);

        mRefreshLayout.addView(view);
//        mRefreshLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                requestManager.request(requestManager.mJavaRetrofitService.getRefreshTips(), new BaseObserverImpl<String>() {
//                    @Override
//                    public void onSuccess(String s) {
//                        super.onSuccess(s);
//                        CustomsRefreshHeader header = (CustomsRefreshHeader) mRefreshLayout.getRefreshHeader();
//                        header.setTips(s);
//                    }
//                });
//                mRefreshLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//            }
//        });

        return mRefreshLayout;
    }
}
